//
//  HomeBuyHeadView.m
//  GiftNote
//
//  Created by lanou on 15/8/14.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "HomeBuyHeadView.h"
#import "HomeHeader.h"
#import "UIImageView+WebCache.h"

@interface HomeBuyHeadView ()<UIScrollViewDelegate>

@end

@implementation HomeBuyHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight / 2)];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kHomeHeight / 2 - 30, kHomeWidth, 30)];
        [self addSubview:_pageControl];
        
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.numberOfLines = 0;
        [self addSubview:_titleLabel];
        
        
        _priceLabel = [[UILabel alloc]init];
        _priceLabel.font = [UIFont systemFontOfSize:15];
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        _priceLabel.textColor = [UIColor redColor];
        [self addSubview:_priceLabel];
        
        
        _desLabel = [[UILabel alloc]init];
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.numberOfLines = 0;
        [self addSubview:_desLabel];
        
    }
    return self;
}


- (void)setBuyModel:(HomeBuyDetailModel *)buyModel
{
    _buyModel = buyModel;
    
    
    
    _pageControl.numberOfPages = _buyModel.image_urls.count;
    
    UIImageView *firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight / 2)];
    [firstImageView sd_setImageWithURL:[NSURL URLWithString:[_buyModel.image_urls lastObject]] placeholderImage:nil];
    [_scrollView addSubview:firstImageView];
    
    if (_buyModel.image_urls.count > 1) {
        _scrollView.contentOffset = CGPointMake(kHomeWidth, 0);
        _scrollView.contentSize = CGSizeMake(kHomeWidth * (_buyModel.image_urls.count + 1), kHomeHeight / 2);
        for (int i = 0; i < _buyModel.image_urls.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kHomeWidth * (i + 1), 0, kHomeWidth, kHomeHeight / 2)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_buyModel.image_urls[i]] placeholderImage:nil];
            [_scrollView addSubview:imageView];
        }
        
    }
    
    _titleLabel.text = _buyModel.name;
    _titleLabel.frame = CGRectMake(10, CGRectGetMaxY(_scrollView.frame) + 5, kHomeWidth - 20, [HomeBuyHeadView heightForName:_buyModel.name]);
    _priceLabel.text = [NSString stringWithFormat:@"￥ %@",_buyModel.price];
    _priceLabel.frame = CGRectMake(10, CGRectGetMaxY(_titleLabel.frame) + 3, kHomeWidth - 20, 20);
    _desLabel.text = _buyModel.desp;
    _desLabel.frame = CGRectMake(10, CGRectGetMaxY(_priceLabel.frame) + 10, kHomeWidth - 20, [HomeBuyHeadView heightForDesp:_buyModel.desp]);
    
    
    _view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_desLabel.frame) + 5, kHomeWidth, 10)];
    _view.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_view];
}

+ (CGFloat)heightForDesp:(NSString *)string
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(kHomeWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightForName:(NSString *)string
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake(kHomeWidth - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightForView:(HomeBuyDetailModel *)buyModel
{
    CGFloat height = kHomeHeight / 2 + [HomeBuyHeadView heightForName:buyModel.name] + 20 + [HomeBuyHeadView heightForDesp:buyModel.desp] + 28;
    return height;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_scrollView.contentOffset.x < 0) {
        _scrollView.contentOffset = CGPointMake(_buyModel.image_urls.count * kHomeWidth, 0);
        
    } else if (_scrollView.contentOffset.x > _buyModel.image_urls.count * kHomeWidth) {
        
        _scrollView.contentOffset = CGPointMake(0, 0);
    
    }
}


@end






